package com.fling.abcde;

import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import com.alibaba.fastjson.JSONObject;

import java.lang.reflect.Field;
import java.util.Arrays;

/**
 * 异常信息的记录
 *
 * @author FengLing
 */
public class ErrorReport implements Thread.UncaughtExceptionHandler {

    private String mBuildJson;
    private Thread.UncaughtExceptionHandler mDefaultHandler;

    private static class ClassHolder {
        private static final ErrorReport INSTANCE = new ErrorReport();
    }

    private ErrorReport() {
        JSONObject jb1 = new JSONObject();
        Field[] declaredFields = Build.class.getDeclaredFields();
        for (Field field : declaredFields) {
            field.setAccessible(true);
            try {
                Object value = field.get(null);
                if (value instanceof String[]) {
                    value = Arrays.toString((Object[]) value);
                }
                jb1.put(field.getName(), value);
            } catch (Exception ignored) {
            }
        }
        JSONObject jb2 = new JSONObject();
        Field[] versionFields = Build.VERSION.class.getDeclaredFields();
        for (Field field : versionFields) {
            field.setAccessible(true);
            try {
                Object value = field.get(null);
                if (value instanceof String[]) {
                    value = Arrays.toString((Object[]) value);
                }
                jb2.put(field.getName(), value);
            } catch (Exception ignored) {
            }
        }

        JSONObject jb = new JSONObject();
        jb.put("Build", jb1);
        jb.put("Build_VERSION", jb2);
        mBuildJson = jb.toString();
        System.out.println("Build--->>" + mBuildJson);
    }

    public static void init() {
        // 获取系统默认的UncaughtException处理器
        ClassHolder.INSTANCE.mDefaultHandler = Thread.getDefaultUncaughtExceptionHandler();
        // 设置该CrashHandler为程序的默认处理器
        Thread.setDefaultUncaughtExceptionHandler(ClassHolder.INSTANCE);
    }

    @Override
    public void uncaughtException(Thread thread, Throwable throwable) {

        if (!handleException(throwable) && mDefaultHandler != null) {
            mDefaultHandler.uncaughtException(thread, throwable);
        } else {
            SystemClock.sleep(1000);
            BaseApplication.safeExitApp();
        }
    }

    private boolean handleException(Throwable throwable) {

        if (throwable == null) {
            return false;
        }
        // new Thread() {
        //
        // 	public void run() {
        //
        // 		Looper.prepare();
        // 		Toast.makeText(sContext, "啊哦，程序出现异常了！", Toast.LENGTH_LONG).show();
        // 		Looper.loop();
        // 	}
        // }.start();
        String msg = mBuildJson + "\n\n" + Log.getStackTraceString(throwable);
        Log.i("666 ", "ErrorReport-->" + msg);
//        HashMap<String, String> map = HashMapPoolUtils.get();
//        map.put("timestamp", String.valueOf(System.currentTimeMillis()));
//        map.put("msg", msg);
//        map.put("sign", SignUtils.getSign(map));
//        new NetManager(null).doPost("this", null);
        return true;
    }
}